package com.czkt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czkt.pojo.BasDict;
import com.czkt.pojo.CstCustomer;
import com.czkt.pojo.CstService;
import com.czkt.pojo.SysUser;
import com.czkt.pojo.vo.SysUserVo;
import com.czkt.service.IBasDictService;
import com.czkt.service.ICstCustomerService;
import com.czkt.service.ICstServiceService;
import com.czkt.service.SysUserService;
import com.czkt.util.PageUtile;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liu
 * @since 2024-06-21
 */
@Controller
@RequestMapping("/service")
public class CstServiceController {


    @Autowired
    ICstServiceService cstServiceService;


    @Autowired
    IBasDictService basDictService;


    @Autowired
    ICstCustomerService cstCustomerService;
    @Autowired
    SysUserService sysUserService;

    /**
     * 获取服务列表
     *
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public Object getPlanList(CstService cstService,
                              @RequestParam(defaultValue = "1") Integer pageIndex,
                              @RequestParam(defaultValue = "4") Integer pageSize,
                              Model model) {

        IPage<CstService> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageIndex);


        //获取当前登录人id
        SysUserVo currentUser = (SysUserVo) SecurityUtils.getSubject().getPrincipal();

        QueryWrapper<CstService> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda().eq(CstService::getSvrCreateId, currentUser.getUsrId());

        IPage<CstService> page1 = cstServiceService.page(page, queryWrapper);

        PageUtile<CstService> pageUtile = new PageUtile();

        BeanUtils.copyProperties(page1, pageUtile);

        model.addAttribute("servicePage", pageUtile);


        //查询客户等级
        QueryWrapper<BasDict> customerLevel = new QueryWrapper<>();
        customerLevel.lambda().eq(BasDict::getDictType, "服务类型");

        model.addAttribute("dicts", basDictService.list(customerLevel));

        return "service/list";

    }


    /**
     * 服务分配
     *
     * @param model
     * @return
     */
    @RequestMapping("/dispatch")
    public Object dispatchPlanList(CstService cstService,
                                   @RequestParam(defaultValue = "1") Integer pageIndex,
                                   @RequestParam(defaultValue = "4") Integer pageSize,
                                   Model model) {

        IPage<CstService> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageIndex);


//        //获取当前登录人id
//        SysUserVo currentUser = (SysUserVo) SecurityUtils.getSubject().getPrincipal();
//
        QueryWrapper<CstService> queryWrapper = new QueryWrapper<>();
//
        queryWrapper.lambda().ne(CstService::getSvrStatus, 4);

        IPage<CstService> page1 = cstServiceService.page(page, queryWrapper);

        PageUtile<CstService> pageUtile = new PageUtile();

        BeanUtils.copyProperties(page1, pageUtile);

        model.addAttribute("servicePage", pageUtile);


        //查询客户等级
        QueryWrapper<BasDict> customerLevel = new QueryWrapper<>();
        customerLevel.lambda().eq(BasDict::getDictType, "服务类型");

        model.addAttribute("dicts", basDictService.list(customerLevel));


        //查询系统维护人员
        QueryWrapper<SysUser> userList = new QueryWrapper<>();
        userList.lambda().eq(SysUser::getUsrRoleId, 4);

        model.addAttribute("users", sysUserService.list(userList));

        return "service/dispatch";

    }


    /**
     * 服务分配
     *
     * @param model
     * @return
     */
    @RequestMapping("/deal")
    public Object dealPlanList(CstService cstService,
                               @RequestParam(defaultValue = "1") Integer pageIndex,
                               @RequestParam(defaultValue = "4") Integer pageSize,
                               Model model) {

        IPage<CstService> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageIndex);


//        //获取当前登录人id
        SysUserVo currentUser = (SysUserVo) SecurityUtils.getSubject().getPrincipal();
//
        QueryWrapper<CstService> queryWrapper = new QueryWrapper<>();
//
        queryWrapper.lambda().in(CstService::getSvrStatus, 2, 3);

        queryWrapper.lambda().eq(CstService::getSvrDealId, currentUser.getUsrId());

        IPage<CstService> page1 = cstServiceService.page(page, queryWrapper);

        PageUtile<CstService> pageUtile = new PageUtile();

        BeanUtils.copyProperties(page1, pageUtile);

        model.addAttribute("page", pageUtile);


        //查询客户等级
        QueryWrapper<BasDict> customerLevel = new QueryWrapper<>();
        customerLevel.lambda().eq(BasDict::getDictType, "服务类型");

        model.addAttribute("dicts", basDictService.list(customerLevel));


        //查询系统维护人员
        QueryWrapper<SysUser> userList = new QueryWrapper<>();
        userList.lambda().eq(SysUser::getUsrRoleId, 4);

        model.addAttribute("users", sysUserService.list(userList));

        return "service/deal";

    }


    /**
     * 定义跳转到新增页的方法
     */
    @RequestMapping("/add")
    public Object toAdd(Model model) {
        //获取当前登录人负责的客户列表
        QueryWrapper<CstCustomer> queryWrapper = new QueryWrapper<>();
        //获取当前登录人id
        SysUserVo currentUser = (SysUserVo) SecurityUtils.getSubject().getPrincipal();
        queryWrapper.lambda()
                .eq(CstCustomer::getCustManagerId, currentUser.getUsrId());
        model.addAttribute("customers", cstCustomerService.list(queryWrapper));
        //获取服务器类型
        QueryWrapper<BasDict> customerLevel = new QueryWrapper<>();
        customerLevel.lambda().eq(BasDict::getDictType, "服务类型");

        model.addAttribute("dicts", basDictService.list(customerLevel));


        return "service/add";
    }


    /**
     * 保存用户信息的方法
     */
    @PostMapping("/save")
    public Object saveUser(CstService cstService) {

        boolean result = cstServiceService.saveOrUpdate(cstService);
        if (result) {
            return "redirect:/service/list";
        } else if (cstService.getSvrId() == null) {
            return "redirect:/service/add";
        } else {
            return "redirect:/service/edit?usrId=" + cstService.getSvrId();
        }
    }


    /**
     * 服务处理
     */
    @PostMapping("/editDeal")
    public Object editDeal(CstService cstService) {
        cstService.setSvrStatus("3");
        boolean result = cstServiceService.updateById(cstService);
        if (result) {
            return "redirect:/service/deal";
        } else {
            return "redirect:/service/edit?svrId=" + cstService.getSvrId();
        }
    }


    /**
     * 服务反馈
     */
    @PostMapping("/editFB")
    public Object editFB(CstService cstService) {
        if (cstService.getSvrSatisfy() < 3) {//如果服务不满意则为待分配
            cstService.setSvrStatus("1");
        } else {
            cstService.setSvrStatus("4");
        }
        boolean result = cstServiceService.updateById(cstService);
        if (result) {
            return "redirect:/service/list";
        } else {
            return "redirect:/service/feedbackEdit?svrId=" + cstService.getSvrId();
        }
    }


    /**
     * 获取用户详细并跳转修改页面
     */
    @RequestMapping("/edit")
    public Object toUpdate(String svrId, Model model) {

        model.addAttribute("serviced", cstServiceService.getById(svrId));


        return "service/dealEdit";
    }

    /**
     * 获取用户详细并跳转修改页面
     */
    @RequestMapping("/feedbackEdit")
    public Object feedbackEdit(String svrId, Model model) {

        model.addAttribute("serviced", cstServiceService.getById(svrId));


        return "service/feedbackEdit";
    }


    /**
     * 分配服务
     */
    @RequestMapping("/allot/{svrId}/{svrDueId}")
    @ResponseBody
    public Object allot(@PathVariable Long svrId,
                        @PathVariable Long svrDueId,
                        String userName
            , Model model) {


        CstService cstService = new CstService();
        //获取当前登录人作为分配人
        SysUserVo currentUser = (SysUserVo) SecurityUtils.getSubject().getPrincipal();

        cstService.setSvrDueId(currentUser.getUsrId());
        cstService.setSvrDueTo(currentUser.getUsrName());

        cstService.setSvrId(svrId);

        cstService.setSvrStatus("2");

        //选择的用户作为处理人
        cstService.setSvrDealId(svrDueId);
        cstService.setSvrDealBy(userName);
        return cstServiceService.updateById(cstService);
    }


    /**
     * 删除用户信息
     */
    @RequestMapping("/del/{svrId}")
    @ResponseBody
    public Object del(@PathVariable() Long svrId, Model model) {
        Map resultMap = new HashMap();
        resultMap.put("delResult", cstServiceService.removeById(svrId));
        return resultMap;
    }


}
